import api from '@/common/request/'
import { getTime } from '@/common/utils/utils'

// uni.removeStorageSync('globalSite');

const state = {
	globalSiteRank: uni.getStorageSync('globalSite').globalSiteRank  != undefined ? uni.getStorageSync('globalSite').globalSiteRank : '',
	globalSiteCode: uni.getStorageSync('globalSite').globalSiteCode  != undefined ? uni.getStorageSync('globalSite').globalSiteCode : '',
	expiration_time: uni.getStorageSync('globalSite').expiration_time != undefined ? uni.getStorageSync('globalSite').expiration_time : '',
    logo: '',
    name: '',
    tel: '',
    theme: '',
    goeasy_open: 0,
    goeasy_appkey: '',
    site: {
		site_name: ''
	}
}

const getters = {
	
}

const actions = {
	getCurrentSite({commit, state}) {
		return new Promise((resolve, reject) => {
			api.get('/api/home.Home/currentLocationCity').then((res)=>{
				if(res.code == 0){
					resolve(res.data.site)
				}else{
					reject()
				}
			})
		});
	},
	getHomePage({commit, state}) {
		return new Promise((resolve, reject) => {
			api.get('/api/home.Setting/homePage').then((res)=>{
				if(res.code == 0){
					commit('setSite', res.data.config)
					resolve(res.data.config)
				}else{
					reject()
				}
			})
		});
	},
	setGlobalSite({commit}, data) {
		return new Promise((resolve, reject) => {
			commit('setGlobalSite', data)
			resolve()
		});
	}
}

const mutations = {
	setGlobalSite(state, data) {
		// console.log(data)
		let time = getTime()
		state.globalSiteRank = data.rank
		state.globalSiteCode = data.id
		// state.expiration_time = time + 10
		state.expiration_time = time + 86400
		
		let globalSite = {}
		globalSite.globalSiteRank = data.rank
		globalSite.globalSiteCode = data.id
		// globalSite.expiration_time = time + 10
		globalSite.expiration_time = time + 86400
		uni.setStorageSync('globalSite', globalSite);
	},
    setSite(state, data) {
		state.logo = data.logo
		state.name = data.name
		state.tel = data.tel
		state.theme = data.theme
		state.goeasy_open = data.goeasy_open
		state.goeasy_appkey = data.goeasy_appkey
		state.site = data.site
    }
}

export default {
    namespaced: true,
    state,
    actions,
    getters,
    mutations
}
